﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Peculiar
{
    public partial class HospitalProcureCata : System.Web.UI.Page
    {
        DataTable dt = new DataTable();
        static Dictionary<string, string> hid = new Dictionary<string, string>();
        protected void Page_Load(object sender, EventArgs e)
        {


            lblHospitalName.Text = "<b>医院名称：</b>" + Server.UrlDecode(Request.QueryString["id2"].ToString());
            if (!IsPostBack)
            {
                BindSort();
                dataBind();
            }
        }

        /// <summary>
        /// 数据查询与绑定
        /// </summary>
        private void dataBind()
        {
            StringBuilder where = new StringBuilder();
            int totalrecord = 0;
            string hospitalId = Request.QueryString["id1"].ToString();
            if (!txtGoodsId.Text.Trim().Equals(""))
            {
                where.Append(" and ProcureCatalogID like '%" + txtGoodsId.Text.Trim() + "%'");
            } if (!txtGoodsName.Text.Trim().Equals(""))
            {
                where.Append(" and ProductName like '%" + txtGoodsName.Text.Trim() + "%'");
            }
            if (DropDownList3.SelectedIndex==1)
            {
                where.Append(" and (b.HHospitalID='"+Request.QueryString["id1"].ToString()+"')");
                btnDelte0.Visible = true;
                btnDelte0.Text = "启用基药";
                btnDelte1.Visible = true;
                btnDelte1.Text = "启用非基药";
                btnDelte.Visible = true;
                btnDelte.Text = "启用";
            }
            else if (DropDownList3.SelectedIndex == 2)
            {
                where.Append(" and  (b.HHospitalID is null or b.HHospitalID<>'"+Request.QueryString["id1"].ToString()+"')");
                btnDelte0.Visible = true;
                btnDelte0.Text = "禁用基药";
                btnDelte1.Visible = true;
                btnDelte1.Text = "禁用非基药";
                btnDelte.Visible = true;
                btnDelte.Text = "禁用";
            }
            else 
            {
                btnDelte0.Visible =false;
                btnDelte.Visible = false;
                btnDelte1.Visible = false;
            }
            if (ddlDrugSort.SelectedIndex!=0)
            {
                  where.Append(" and SortName='"+ddlDrugSort.SelectedItem+"'");
            }
            if (DropDownList2.SelectedIndex==1)
            {
                where.Append("and SortSource=0");
            }
            if (DropDownList2.SelectedIndex==2)
            {
                where.Append("and SortSource=1");
            }
            HZ.Supervise.IBLL.IBLL_SpecialAuthority sp = HZ.Supervise.BLLFactory.IBLL_SpecialAuthority();
            dt = sp.findHospitalGoodsByWhere(where.ToString(),hospitalId, AspNetPager1.CurrentPageIndex, 12, out totalrecord);
            if (hid.Count > 0) hid.Clear();
            gvwProduceCatalog.DataSource = dt;
            gvwProduceCatalog.DataBind();
            AspNetPager1.RecordCount = totalrecord;
        }
        /// <summary>
        /// 查看是否禁用
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool CheckIsUsing(object obj) 
        {
            bool res = false;
            if (obj.ToString().Equals(Request.QueryString["id1"].ToString()))
            {
                res = true;
            }else
            {
                res = false;
            }
          
            return res;
        }
        /// <summary>
        /// 绑定药品类别
        /// </summary>
        private void BindSort()
        {
            HZ.Supervise.BLL.BLL_Sort bllSort = new HZ.Supervise.BLL.BLL_Sort();
            DataTable dtSort = bllSort.GetSort();
            ddlDrugSort.DataSource = dtSort;
            ddlDrugSort.DataTextField = "SortName";
            ddlDrugSort.DataValueField = "SortID";
            ddlDrugSort.DataBind();

            ddlDrugSort.Items.Insert(0, "---请选择---");
        }

        protected void btnConfirmMessage_Click(object sender, EventArgs e)
        {
            
         // IBLL.IBLL_SpecialAuthority bll_special=new BLL.BLL_SpecialAuthority();
         // string[] goodsId = goodsIds.TrimEnd(',').Split(',');
         // string[] goodsname = hdfRecivedGoodsName.Value.TrimEnd(',').Split(',');
         // string Allgoods = "";
         // IList<HospitalGoods> hospitalGoodsList = new List<HospitalGoods>();
         // for (int i = 0; i < goodsId.Length; i++)
         // {
         //     HospitalGoods hospitalGoods = new HospitalGoods();
         //     hospitalGoods.HGoodsId = goodsId[i].ToString();
         //     hospitalGoods.HHospitalID = Request.QueryString["id1"].ToString();
         //     Allgoods += goodsname[i].ToString() + "("+goodsId[i].ToString()+"),";
         //     hospitalGoods.Huserid = CurrentAccount.GetUser().UserID;
         //     hospitalGoodsList.Add(hospitalGoods);
         // }
         //int res= bll_special.InsertHospitalGoods(hospitalGoodsList);
         //HospitalGoodsLog hospitalGoodsLog = new HospitalGoodsLog();
         //hospitalGoodsLog.HGoods = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "禁用了药品，药品名称如下：</b></br>" + Allgoods.TrimEnd(',') + "</br>";
         //hospitalGoodsLog.HHospitalID = Request.QueryString["id1"].ToString();
         //hospitalGoodsLog.HuserId = CurrentAccount.GetUser().UserID;
         //hospitalGoodsLog.Id = Guid.NewGuid().ToString();
         //bll_special.InsertHospitalGoodsLog(hospitalGoodsLog);
         //if (res > 0)
         //{
         //    this.Page.RegisterStartupScript("", "<script>alert('禁用药品成功!')</script>");
         //    dataBind();
         //}
         //else 
         //{
         //    this.Page.RegisterStartupScript("", "<script>alert('禁用药品失败!')</script>");
         //}
        }

        protected void btnDelte_Click(object sender, EventArgs e)
        {
            if (DropDownList3.SelectedIndex == 1)
            {
                IList<HospitalGoods> hospitalGoodsList = new List<HospitalGoods>();
                string goodsName = "";
                for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
                {
                    CheckBox checkbox = ((CheckBox)gvwProduceCatalog.Rows[i].Cells[0].FindControl("CheckBox1"));
                    if (checkbox.Checked)
                    {
                        HospitalGoods hospitalgoods = new HospitalGoods();
                        hospitalgoods.HGoodsId = gvwProduceCatalog.Rows[i].Cells[1].Text;
                        goodsName += gvwProduceCatalog.Rows[i].Cells[2].Text + "(" + gvwProduceCatalog.Rows[i].Cells[1].Text + "),";
                        hospitalgoods.HHospitalID = Request.QueryString["id1"].ToString();
                        hospitalgoods.Isfolg = true;
                        hospitalGoodsList.Add(hospitalgoods);
                    }
                }
                IBLL.IBLL_SpecialAuthority bll_special = new BLL.BLL_SpecialAuthority();
                int res = bll_special.DeleteHospitalGoods(hospitalGoodsList);
                HospitalGoodsLog hospitalGoodsLog = new HospitalGoodsLog();
                hospitalGoodsLog.HGoods = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "启用了药品，药品名称如下：</B></br>" + goodsName.TrimEnd(',') + "</br>";
                hospitalGoodsLog.HHospitalID = Request.QueryString["id1"].ToString();
                hospitalGoodsLog.HuserId = CurrentAccount.GetUser().UserID;
                hospitalGoodsLog.Id = Guid.NewGuid().ToString();
                bll_special.InsertHospitalGoodsLog(hospitalGoodsLog);
                if (res > 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('启用药品成功!')</script>");
                    dataBind();
                }
                else
                {
                    this.Page.RegisterStartupScript("", "<script>alert('启用药品失败!')</script>");
                }
            }
            else 
            {
                IBLL.IBLL_SpecialAuthority bll_special = new BLL.BLL_SpecialAuthority();
                string goodsName = "";
                IList<HospitalGoods> hospitalGoodsList = new List<HospitalGoods>();
                for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
                {
                    CheckBox checkbox = ((CheckBox)gvwProduceCatalog.Rows[i].Cells[0].FindControl("CheckBox1"));
                    
                    if (checkbox.Checked)
                    {
                        HospitalGoods hospitalgoods = new HospitalGoods();
                        hospitalgoods.HGoodsId = gvwProduceCatalog.Rows[i].Cells[1].Text;
                        goodsName += gvwProduceCatalog.Rows[i].Cells[2].Text + "(" + gvwProduceCatalog.Rows[i].Cells[1].Text + "),";
                        hospitalgoods.HHospitalID = Request.QueryString["id1"].ToString();
                        hospitalGoodsList.Add(hospitalgoods);
                    }
                }
                int res = bll_special.InsertHospitalGoods(hospitalGoodsList);
                HospitalGoodsLog hospitalGoodsLog = new HospitalGoodsLog();
                hospitalGoodsLog.HGoods = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "禁用了药品，药品名称如下：</b></br>" + goodsName.TrimEnd(',')+ "</br>";
                hospitalGoodsLog.HHospitalID = Request.QueryString["id1"].ToString();
                hospitalGoodsLog.HuserId = CurrentAccount.GetUser().UserID;
                hospitalGoodsLog.Id = Guid.NewGuid().ToString();
                bll_special.InsertHospitalGoodsLog(hospitalGoodsLog);
                if (res > 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('禁用药品成功!')</script>");
                    dataBind();
                }
                else
                {
                    this.Page.RegisterStartupScript("", "<script>alert('禁用药品失败!')</script>");
                }
            
            }
          
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            dataBind();
        }

        protected void btnConfirmMessage0_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            dataBind();
        }

        protected void btnDelte0_Click(object sender, EventArgs e)
        {
         
            IBLL.IBLL_SpecialAuthority bll_special = new BLL.BLL_SpecialAuthority();   
            if (DropDownList3.SelectedIndex == 1)
            {
                //启用药品
                if (gvwProduceCatalog.Rows.Count == 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('没有要启用的药品!')</script>");
                    return;
                }
                 int res=bll_special.DeleteAllHospitalGoods(Request.QueryString["id1"].ToString(),0);
                 HospitalGoodsLog hospitalGoodsLog = new HospitalGoodsLog();
                 hospitalGoodsLog.HGoods = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "批量启用了基药药品</br>";
                 hospitalGoodsLog.HHospitalID = Request.QueryString["id1"].ToString();
                 hospitalGoodsLog.HuserId = CurrentAccount.GetUser().UserID;
                 hospitalGoodsLog.Id = Guid.NewGuid().ToString();
                 bll_special.InsertHospitalGoodsLog(hospitalGoodsLog);
                 if (res > 0)
                 {
                     this.Page.RegisterStartupScript("", "<script>alert('启用基药药品成功!')</script>");
                     
                     dataBind();
                 }
                 else
                {
                    this.Page.RegisterStartupScript("", "<script>alert('启用基药药品失败!')</script>");
                }
            }
            else
            {
                if (gvwProduceCatalog.Rows.Count == 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('没有要禁用的药品!')</script>");
                    return;
                }
                //禁用药品
                Model.HospitalGoods hospitalGoods = new HospitalGoods();
                hospitalGoods.HHospitalID = Request.QueryString["id1"].ToString();
                hospitalGoods.Huserid = CurrentAccount.GetUser().UserID;
               int res= bll_special.UpdateAllHospitalGoods(hospitalGoods,0);
               HospitalGoodsLog hospitalGoodsLog = new HospitalGoodsLog();
               hospitalGoodsLog.HGoods = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "批量禁用了基药药品</br>";
               hospitalGoodsLog.HHospitalID = Request.QueryString["id1"].ToString();
               hospitalGoodsLog.HuserId = CurrentAccount.GetUser().UserID;
               hospitalGoodsLog.Id = Guid.NewGuid().ToString();
               bll_special.InsertHospitalGoodsLog(hospitalGoodsLog);
               if (res > 0)
               {
                   this.Page.RegisterStartupScript("", "<script>alert('禁用基药药品成功!')</script>");
                   dataBind();
               }
               else 
               {
                   this.Page.RegisterStartupScript("", "<script>alert('禁用基药药品失败!')</script>");
               }
            }
        }

        protected void btnDelte1_Click(object sender, EventArgs e)
        {
  
            IBLL.IBLL_SpecialAuthority bll_special = new BLL.BLL_SpecialAuthority();
            if (DropDownList3.SelectedIndex == 1)
            {
                //启用药品
                if (gvwProduceCatalog.Rows.Count == 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('没有要启用的药品!')</script>");
                    return;
                }
                int res = bll_special.DeleteAllHospitalGoods(Request.QueryString["id1"].ToString(),1);
                HospitalGoodsLog hospitalGoodsLog = new HospitalGoodsLog();
                hospitalGoodsLog.HGoods = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "批量启用了非基药药品</br>";
                hospitalGoodsLog.HHospitalID = Request.QueryString["id1"].ToString();
                hospitalGoodsLog.HuserId = CurrentAccount.GetUser().UserID;
                hospitalGoodsLog.Id = Guid.NewGuid().ToString();
                bll_special.InsertHospitalGoodsLog(hospitalGoodsLog);
                if (res > 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('启用非基药药品成功!')</script>");

                    dataBind();
                }
                else
                {
                    this.Page.RegisterStartupScript("", "<script>alert('启用非基药药品失败!')</script>");
                }
            }
            else
            {
                if (gvwProduceCatalog.Rows.Count == 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('没有要禁用的药品!')</script>");
                    return;
                }
                //禁用药品
                Model.HospitalGoods hospitalGoods = new HospitalGoods();
                hospitalGoods.HHospitalID = Request.QueryString["id1"].ToString();
                hospitalGoods.Huserid = CurrentAccount.GetUser().UserID;
                int res = bll_special.UpdateAllHospitalGoods(hospitalGoods,1);
                HospitalGoodsLog hospitalGoodsLog = new HospitalGoodsLog();
                hospitalGoodsLog.HGoods = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "批量禁用了非基药药品</br>";
                hospitalGoodsLog.HHospitalID = Request.QueryString["id1"].ToString();
                hospitalGoodsLog.HuserId = CurrentAccount.GetUser().UserID;
                hospitalGoodsLog.Id = Guid.NewGuid().ToString();
                bll_special.InsertHospitalGoodsLog(hospitalGoodsLog);
                if (res > 0)
                {
                    this.Page.RegisterStartupScript("", "<script>alert('禁用非基药药品成功!')</script>");
                    dataBind();
                }
                else
                {
                    this.Page.RegisterStartupScript("", "<script>alert('禁用非基药药品失败!')</script>");
                }
            }
        }
    }
}
